package com.crimsec.utils.dinfo.impl.xml;

import java.net.URI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name = "file")
@XmlType(name = "item")
public class NewsItem {
    
    public static class ShortDateFormatter extends XmlAdapter<String, Date> {
        
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        
        public Date unmarshal(String date) throws Exception {
            return formatter.parse(date);
        }
        
        public String marshal(Date date) throws Exception {
            return formatter.format(date);
        }
    }
    private String name;
    private Date ctime;
    private URI uri;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    @XmlJavaTypeAdapter(ShortDateFormatter.class)
    public Date getCtime() {
        return ctime;
    }
    
    public void setCtime(Date ctime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String d = sdf.format(ctime);
        
        this.ctime = sdf.parse(d);
    }
    
    public URI getUri() {
        return uri;
    }
    
    public void setUri(URI uri) {
        this.uri = uri;
    }
    
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 71 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 71 * hash + (this.ctime != null ? this.ctime.hashCode() : 0);
        hash = 71 * hash + (this.uri != null ? this.uri.hashCode() : 0);
        return hash;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final NewsItem other = (NewsItem) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if (this.ctime != other.ctime && (this.ctime == null || !this.ctime.equals(other.ctime))) {
            return false;
        }
        if (this.uri != other.uri && (this.uri == null || !this.uri.equals(other.uri))) {
            return false;
        }
        return true;
    }
}
